<template>
<pre>
{{str}}
</pre>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'

export default defineComponent({
    setup () {
        const str = ref<string>(`
    import { getTreeByDictTypeCodeList, getOrgAllClue, orgByUserList, selectProvinceAllList } from '@/service/comm/comm'
    import { getLocalSchoolList } from '@/service/comm/schoolCollage'
    import { dictTreeFormat } from '@/service/utils/dictFormat'
    import { SeniorSearchInterFace } from 'wd-platform-library/lib/src/ISeniorSearch/src/ISeniorSearchInterface'
    import { isFormArry } from './addStudentInterface'

    // 高级搜索配置
    interface seniorRequestDataInterface {
        [index: string]: ()=>any;
    }
    // 字典和字段对应关系{key:value} key:当前项的字段  value:对应字典code
    const seniorSearchDict: { [index: string]: string } = {
        // 学员等级
        studentLevel: 'student_level',
        // 学员分类
        studentCategory: 'student_category',
        // 意向度
        intentionLevel: 'intention_level',
        // 考试年份
        examYear: 'exam_year',
        // 入学年份
        enterYear: 'enter_year',
        // 学员类型
        studentType: 'student_type',
        // 就业状态
        workStatus: 'work_status',
        // 从事行业
        workIndustry: 'work_industry',
        // 来源渠道
        sourceChannel: 'source_channel',
        // 学历
        education: 'education',
        // 学制
        educationalSystem: 'educational_system',
        // 联系人关系
        contactRelation: 'contact_relation',
        // 跟进阶段
        processStage: 'process_stage'
    }
    // 下拉框请求数据配置 key:():any[]=>{}    
    // key:对应当前字段
    // ()=>{}: 调用接口/自定义返回数据    
    const seniorRequestData: seniorRequestDataInterface = {
        // 性别
        sex: () => {
            return Promise.resolve([{ label: '未知', value: '0' }, { label: '男', value: '1' }, { label: '女', value: '2' }])
        },
        // 锁定
        lockFlag: () => {
            return Promise.resolve([{ label: '未锁定', value: '0' }, { label: '锁定', value: '1' }])
        },
        // 线索来源(有接口)
        clueSourceCode: async () => {
            const orgAllClueResult = await getOrgAllClue()
            if (orgAllClueResult) {
                return orgAllClueResult.result
            }
        },
        // // 渠道来源(不要)
        // channelSourceId: async () => {
        //     return Promise.resolve([])
        // },
        // 线索分组
        groupId: async () => {
            const orgAllClueResult = await getOrgAllClue()
            if (orgAllClueResult) {
                return orgAllClueResult.result
            }
        },
        // 分配人
        allocUserId: async () => {
            const resultData = await orgByUserList()
            if (resultData) {
                return resultData.result.map(item => {
                    return {
                        label: item.userName,
                        value: item.id
                    }
                })
            }
        },
        // 所在地
        provinceCity: async () => {
            const provinceData = await selectProvinceAllList()
            if (provinceData) {
                return provinceData.result
            }
        },
        // 录入方式
        createType: async () => {
            return Promise.resolve([
                { label: '咨询师录入', value: '1' },
                { label: '咨询师导入', value: '2' },
                { label: '部门管理员录入', value: '3' },
                { label: '部门管理员导入', value: '4' },
                { label: '机构管理员录入', value: '5' },
                { label: '机构管理员导入', value: '5' },
                { label: '渠道分配', value: '6' },
                { label: '跨机构校区转移', value: '7' },
                { label: '跨机构分享', value: '8' }
            ])
        },
        // 就读院校
        nowSchoolId: async () => {
            const resultData = await getLocalSchoolList()
            if (resultData) {
                return resultData.result.map(item => { return { label: item.name, value: item.id } })
            }
        }
    }
    // 1.传递字典codeList,返回codeList对象集合
    // 2.根据接口返回对象和字典字段关系动态生成seniorRequestData
    const requestDict = async (codeList:string[], seniorRequestData: seniorRequestDataInterface):Promise<void> => {
        if (codeList.length > 0) {
            const resultData = await getTreeByDictTypeCodeList(codeList)
            if (resultData) {
                for (const [key, value] of Object.entries(resultData.result)) {
                    Object.keys(seniorSearchDict).forEach(item => {
                        if (key === seniorSearchDict[item]) {
                            seniorRequestData[item] = () => {}
                            seniorRequestData[item] = () => {
                                return Promise.resolve(dictTreeFormat(value, key))
                            }
                        }
                    })
                }
            }
        }
    }
    //该方法高级搜索数据
    const formatSeniorSearch = async (seniorSearchOption:SeniorSearchInterFace<any, any>) => {
        const dictList:string[] = []
        //提取基本搜索的字典类型
        seniorSearchOption.basicForm.fieldDataList.forEach(item => {
            //判断字段是否和上面配置的字典cocde对应
            if (item.field in seniorSearchDict) {
                //如果对应则吧字典类型保存下来
                dictList.push(seniorSearchDict[item.field])
            }
        })
        //提取弹框内搜索的字典类型
        seniorSearchOption.seniorForm && (
            seniorSearchOption.seniorForm.fieldDataList.forEach(item => {
                  //判断字段是否和上面配置的字典cocde对应
                if (item.field in seniorSearchDict) {
                    //如果对应则吧字典类型保存下来
                    dictList.push(seniorSearchDict[item.field])
                }
            }))
        //统一请求字典接口
        await requestDict(dictList, seniorRequestData)
        //调用基本搜索内的需要请求数据的接口
        seniorSearchOption.basicForm.fieldDataList.forEach(item => {
            if (item.type && isFormArry(item.type) === true && seniorRequestData[item.field]) {
                seniorRequestData[item.field]().then((res:any) => {
                    item.dataList = res
                })
            }
        })
        //调用弹框搜索内的需要请求数据的接口
        seniorSearchOption.seniorForm && (seniorSearchOption.seniorForm.fieldDataList.forEach(item => {
            if (item.type && isFormArry(item.type) === true && seniorRequestData[item.field]) {
                seniorRequestData[item.field]().then((res:any) => {
                    item.dataList = res
                })
            }
        }))
    }

    export {
        formatSeniorSearch,
        seniorSearchDict
    }`)
        return {
            str
        }
    }
})
</script>
